package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.article.IFollowClient;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.Dtos.AllDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.model.wemedia.utils.AppThreadLocalUtils;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.FanMapper;
import com.heima.user.mapper.FollowMapper;
import com.heima.user.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class FollowServiceImpl extends ServiceImpl<FollowMapper, ApUserFollow> implements FollowService {

    @Autowired
    private IFollowClient followClient;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private FanMapper fanMapper;

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private CacheService cacheService;

    /**
     * 关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userFollow(AllDto dto) {

        if (dto.getAuthorId() == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Integer userId = AppThreadLocalUtils.getUser().getId();

        //获取作者信息
        ApArticle apArticle = followClient.selectOne(dto);


        if (dto.getOperation() == 1){

            cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION+userId+apArticle.getAuthorId(),"follow");
            cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION+apArticle.getAuthorId()+userId,"fans");

            //删除关注关系
            followMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId,apArticle.getAuthorId()));
            //删除粉丝表数据
            fanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,userId));
        }else {

            ApUserFollow userFollow = followMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, apArticle.getAuthorId()));
            if (userFollow != null){
                return ResponseResult.okResult("该用户已经关注");
            }

            cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION+userId+apArticle.getAuthorId(),"follow",System.currentTimeMillis());
            cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION+apArticle.getAuthorId()+userId,"fans",System.currentTimeMillis());
            //封装关注表数据
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(userId);
            apUserFollow.setFollowId(apArticle.getAuthorId());
            apUserFollow.setFollowName(apArticle.getAuthorName());
            apUserFollow.setCreatedTime(new Date());

            this.save(apUserFollow);

            //封装粉丝表数据

            ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId, AppThreadLocalUtils.getUser().getId()));

            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(apArticle.getAuthorId());
            apUserFan.setFansId(AppThreadLocalUtils.getUser().getId());
            apUserFan.setFansName(apUser.getName());
            apUserFan.setCreatedTime(new Date());

            fanMapper.insert(apUserFan);
        }

        return ResponseResult.okResult("成功");
    }
}
